package best.driver.android.View;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import best.driver.android.BestApp;
import best.driver.android.R;


/**
 * Created by xuefeng on 2014/7/11.
 */
public class ConfirmDialog {
    final Context context;
    public Dialog ad;
    TextView mTvHint;
    Button mBtnConfirm;
    Button mBtnCancel;
    View mDivider;
    TextView mTvTitle;
    private ConfirmDialogListener mListener;
    private DialogDismissListener mDismissListener;

    public boolean mConfirmDismiss = true;

    public ConfirmDialog(final Context context, String hint) {

        this.context = context;
        ad = new Dialog(context, R.style.dialog) {
            @Override
            public void dismiss() {
                if(mDismissListener != null) {
                    mDismissListener.onDismiss();
                }
                super.dismiss();
            }
        };
        //ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
        ad.show();
        ad.setCancelable(true);
        //替换对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_confirm);
        mTvHint = (TextView) window.findViewById(R.id.tv_hint);
        mBtnConfirm = (Button) window.findViewById(R.id.btn_ok);
        mBtnCancel = (Button) window.findViewById(R.id.btn_cancel);
        mTvTitle = (TextView) window.findViewById(R.id.tv_title);
        mDivider = window.findViewById(R.id.view_divider);
        if(!TextUtils.isEmpty(hint)) {
            mTvHint.setText(hint);
        }
        setCancelOut();

        mBtnCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        mBtnConfirm.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mListener != null) {
                    mListener.onClickConfirm();
                }
                if(mConfirmDismiss) {
                    dismiss();
                }
            }
        });
    }

    public void setListener(ConfirmDialogListener listener) {
        this.mListener = listener;
    }
    public void setDismissListener(DialogDismissListener listener) {
        this.mDismissListener = listener;
    }
    public void showOnlyOk() {
        mBtnCancel.setVisibility(View.GONE);
        mDivider.setVisibility(View.GONE);
    }

    public void setTitle(String title) {
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
    }

    public void setTitle(String title, int color) {
        mTvTitle.setVisibility(View.VISIBLE);
        mTvTitle.setText(title);
        mTvTitle.setTextColor(BestApp.getInstance().getResources().getColor(color));
    }

    public void setContent(String content) {
        mTvHint.setText(content);
    }

    public void setConfirmName(String name) {
        mBtnConfirm.setText(name);
    }

    public void dismiss() {
        ad.dismiss();
    }

    public void setCancelNotClicked() {
        ad.setCanceledOnTouchOutside(false);
    }

    public void setCancelOut() {
        ad.setCanceledOnTouchOutside(true);
    }

    public void setConfirmDismiss(boolean dismiss) {
        mConfirmDismiss = dismiss;
    }
}
